<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item size="mini" label="" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入房屋空间编码"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini" label="" prop="fnameL2">
        <el-input
          v-model="queryParams.fnameL2"
          placeholder="请输入房屋空间名称"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-button
          v-hasPermi="['system:post:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="small" :data="roomSpaceList" height="calc(100vh - 150px)">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="编码" align="center" prop="fnumber" min-width="60" />
      <el-table-column label="名称" align="center" prop="fnameL2" min-width="100" />
      <el-table-column label="描述" align="center" min-width="160">
        <template slot-scope="scope">
          <span>{{ scope.row.fdescriptionL2 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorName" min-width="80" />
      <el-table-column label="创建时间" align="center" prop="fcreatetime" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.fcreatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updaterName" min-width="80" />
      <el-table-column label="最后修改时间" align="center" prop="fcreatetime" min-width="120">
        <template slot-scope="scope">
          <span>{{ scope.row.flastupdatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改房屋空间对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入编码名称" maxlength="80" />
        </el-form-item>
        <el-form-item label="名称" prop="fnameL2">
          <el-input v-model="form.fnameL2" placeholder="请输入房屋空间名称" maxlength="255" />
        </el-form-item>
        <el-form-item label="备注" prop="fdescriptionL2">
          <el-input v-model="form.fdescriptionL2" type="textarea" maxlength="255" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRoomSpace, delRoomSpace, addRoomSpace, updateRoomSpace } from '@/api/qualityManage/roomSpace'
export default {
  name: 'RoomSpace',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 房屋空间表格数据
      roomSpaceList: [],
      originalList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      isAdd: true,
      // 查询参数
      queryParams: {
        fnumber: undefined,
        fnameL2: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fnameL2: [
          { required: true, message: '房屋空间名称不能为空', trigger: 'blur' }
        ],
        fnumber: [
          { required: true, message: '房屋空间编码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询房屋空间列表 */
    getList() {
      this.loading = true
      listRoomSpace(this.queryParams).then(response => {
        this.roomSpaceList = response.data
        this.originalList = [...response.data]
        this.loading = false
        this.handleQuery()
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        fid: undefined,
        fnumber: undefined,
        fnameL2: undefined,
        fdescriptionL2: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.getList()
      if (this.queryParams.fnumber) {
        this.roomSpaceList = this.originalList.filter(type => type.fnumber && type.fnumber.indexOf(this.queryParams.fnumber) !== -1)
      } else {
        this.roomSpaceList = this.originalList
      }
      if (this.queryParams.fnameL2) {
        this.roomSpaceList = this.roomSpaceList.filter(type => type.fnameL2 && type.fnameL2.indexOf(this.queryParams.fnameL2) !== -1)
      }
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加房屋空间'
      this.isAdd = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.open = true
      this.title = '修改房屋空间'
      this.isAdd = false
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (!this.isAdd) {
            for (let i = 0; i < this.originalList.length; i++) {
              if (this.originalList[i].fnumber === this.form.fnumber && this.originalList[i].fid !== this.form.fid) {
                this.$message.warning('编码已存在，不能重复')
                return
              }
            }
            updateRoomSpace(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            for (let i = 0; i < this.originalList.length; i++) {
              if (this.originalList[i].fnumber === this.form.fnumber) {
                this.$message.warning('编码已存在，不能重复')
                return
              }
            }
            addRoomSpace(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fnameL2 = row.fnameL2
      this.$confirm('是否确认删除房屋空间名称为"' + fnameL2 + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delRoomSpace(row.fid)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
